﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Finance;
using Reposotory.Finance;
using Model.Finance.DTO;
using Model.Util;
using Repository.Util;

namespace Services.Finance
{
    public class AccountBalanceServices : Service
    {
        IRepositoryAsync<AccountBalance> m_AccountBalanceRepository;
        IRepositoryAsync<Identity> m_IdentityRepository;
        public AccountBalanceServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_AccountBalanceRepository = m_UnitOfWork.RepositoryAsync<AccountBalance>();
            m_IdentityRepository = m_UnitOfWork.RepositoryAsync<Identity>();
        }

        public int AddAccountBalance(string userId,decimal balanceMoney,int buinessCode,int? orgaCode,string relationId) {
            AccountBalance balance = new AccountBalance();
            balance.BalanceMoney = balanceMoney;
            balance.BuinessCode = buinessCode;
            balance.CreateTime = DateTime.Now;
            balance.EntityState = EntityState.Added;
            balance.OrgaCode = orgaCode;
            balance.RelationId = relationId;
            balance.UserId = userId;
            m_AccountBalanceRepository.Insert(balance);
            if (m_UnitOfWork.SaveChanges() == 1)
            {
                return 1;
            }
            return 0;
        }

        public void DBC_AddAccountBalance(string userId, decimal balanceMoney, int buinessCode, int? orgaCode, string relationId)
        {
            AccountBalance balance = new AccountBalance();
            balance.BalanceMoney = balanceMoney;
            balance.BuinessCode = buinessCode;
            balance.CreateTime = DateTime.Now;
            balance.EntityState = EntityState.Added;
            balance.OrgaCode = orgaCode;
            balance.RelationId = relationId;
            balance.UserId = userId;
            m_AccountBalanceRepository.Insert(balance);
        }

        public List<AccountBalanceModel> GetMyAccountBalanceList(int? offset, int? limit, string userId,string BalanceCode)
        {
            return m_AccountBalanceRepository.GetMyAccountBalance(offset.Value, limit.Value, userId,BalanceCode);
        }
        /// <summary>
        /// 创建流水业务码
        /// </summary>
        /// <param name="pre"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public string CreateBalanceCode(string pre, string projectId)
        {
            Identity iden = new Identity();
            iden.CreateTime = DateTime.Now;
            iden.EntityState = EntityState.Added;
            iden.ProjectID = projectId;
            iden.Guid = Guid.NewGuid().ToString();
            m_IdentityRepository.Insert(iden);
            m_UnitOfWork.SaveChanges();
            Identity nid = m_IdentityRepository.Get(iden.Guid);
            string year = DateTime.Now.Year.ToString().Substring(2, 2);
            string Monty = DateTime.Now.Month.ToString();
            string day = DateTime.Now.Day.ToString();

            return pre + year + Monty + day + nid.ID.ToString();
        }
    }
}
